package com.youth.userservice.controller;
import com.youth.ClientEntity.UserEntity;
import com.youth.commonutils.*;
import com.youth.userservice.entity.UcenterMember;
import com.youth.userservice.entity.vo.LoginVo;
import com.youth.userservice.entity.vo.RegisterVo;
import com.youth.userservice.entity.vo.SearchObj;
import com.youth.userservice.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author huangqicai
 * @since 2022-10-27
 */
@Api(description = "手动输入信息登录注册")
//使用nginx是需要使用@CrossOrigin解决跨域问题的，现在使用gateway网关替代
@RestController
@RequestMapping("/userservice/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "实现用户注册")
    @PostMapping("/userRegister")
    public R userRegister(@RequestBody RegisterVo registerVo) {
        return memberService.userRegister(registerVo);
    }


    /*实现用户登录的方法*/
    /*返回的是一个token*/
    @ApiOperation(value = "实现用户登录")
    @PostMapping("/userLogin")
    public R userLogin(@RequestBody LoginVo loginVo) {
        String tokenStr = memberService.userLogin(loginVo);
        return R.ok().data("token", tokenStr);
    }

    /**
     * 退出登录，删除redis缓存
     *
     * @return
     */
    @GetMapping("userLogout")
    public R userLogOut(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserDto dto = JwtUtils.getUserDtoByToken(token);
//        删除缓存中的值
        Boolean delete = redisTemplate.delete(dto.getNickName());
        return R.ok().success(delete);
    }

    /*根据token获取用户信息*/
    @ApiOperation(value = "根据request请求获取用户信息")
    @GetMapping("/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request) {
        /*获取到token里面的用户id*/
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        /*根据用户id获取用户登录信息*/
        return memberService.getLoginInfo(memberIdByJwtToken);
    }

    @ApiOperation(value = "根据userId获取用户信息（供远程调用）")
    @GetMapping("/getLoginInfoByUserId/{userId}")
    public UserDto getLoginInfoByToken(@PathVariable("userId") String userId) {
        UcenterMember member = memberService.getById(userId);
        /*转换vo结果类*/
        return new UserDto(member.getId(), member.getNickname(), member.getAvatar());
    }

    /*根据memberId获取member的name(误删-huang)*/
    @ApiOperation(value = "根据memberId获取member的name")
    @GetMapping("/ByIdGetName/{memberId}")
    public String ByIdGetName(@PathVariable(value = "memberId") String memberId) {
        return memberService.ByIdGetName(memberId);
    }

    /*根据memberId获取member的信息(误删-huang)*/
    @ApiOperation(value = "根据memberId获取member的信息")
    @PostMapping("/ByIdGetUser/")
    public UserEntity ByIdGetUser(@RequestBody HashMap<String, String> map) {
        return memberService.ByIdGetUser(map);
    }

    /*根据用户id和金额去扣减金额*/
    @ApiOperation(value = "根据用户id和金额去扣减金额-huang-远程")
    @GetMapping("/subtract/{userId}/{money}")
    public boolean subtract(@PathVariable(value = "userId") String userId,
                            @PathVariable(value = "money") String money) {
        return memberService.subtractMoney(userId, money);
    }


    @ApiOperation(value = "获取用户后台列表信息")
    @PostMapping("/{pageNum}/{pageSize}")
    public R pageList(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize,
                      @RequestBody SearchObj searchObj) {
        return memberService.pageList(pageNum, pageSize, searchObj);
    }

    @ApiOperation(value = "根据学生id进行删除，同时删除学生相关信息")
    @DeleteMapping("{id}")
    public R deleteStudent(@PathVariable("id") String id) {
        return memberService.removeStudent(id);
    }

    @ApiOperation(value = "将学生加入或移除黑名单")
    @PutMapping("{id}/{isDisabled}")
    public R changeDisabledStatus(@PathVariable("id") String id, @PathVariable("isDisabled") Boolean isDisabled) {
        return memberService.changeDisabledStatus(id, isDisabled);
    }


    @ApiOperation(value = "修改学生信息")
    @PostMapping("/update")
    public R update(@RequestBody RegisterVo registerVo) {
        return memberService.updateUserInfo(registerVo);
    }


}

